package com.abso.mp3tunes.locker.core.data;

import java.util.Date;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.dom4j.Element;

/**
 * An album is composed by a set of tracks stored on the Locker.
 */
public final class Album extends AbstractLockerData {

	/** The unique identifier. */
	private String id;

	/** The title. */
	private String title;

	/** The identifier of the artist. */
	private String artistId;

	/** The name of the artist. */
	private String artistName;

	/** The number of tracks composing the album. */
	private int trackCount;

	/** The total size (in bytes) of the tracks included on the album. */
	private long size;

	/** The release date of the album. */
	private Date releaseDate;

	/** The purchase date of the album. */
	private Date purchaseDate;

	/** The status code indicating if the album is associated with album's art. */
	private int hasArt;

	/**
	 * Constructs a new album.
	 * 
	 * @param item
	 *            the &lt;item&gt; XML element.
	 */
	public Album(Element item) {
		this.id = LockerDataUtils.getString(item, "albumId");
		this.title = LockerDataUtils.getString(item, "albumTitle");
		this.artistId = LockerDataUtils.getString(item, "artistId");
		this.artistName = LockerDataUtils.getString(item, "artistName");
		this.trackCount = LockerDataUtils.getInt(item, "trackCount", -1);
		this.size = LockerDataUtils.getLong(item, "albumSize", -1L);
		this.releaseDate = LockerDataUtils.getDate(item, "releaseDate");
		this.purchaseDate = LockerDataUtils.getDate(item, "releaseDate");
		this.hasArt = LockerDataUtils.getInt(item, "hasArt", -1);
	}

	/**
	 * Returns the unique identifier.
	 * 
	 * @return the unique identifier.
	 */
	public String getId() {
		return id;
	}

	/**
	 * Returns the title.
	 * 
	 * @return the title.
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Returns the identifier of the artist.
	 * 
	 * @return the identifier of the artist.
	 */
	public String getArtistId() {
		return artistId;
	}

	/**
	 * Returns the name of the artist.
	 * 
	 * @return the name of the artist.
	 */
	public String getArtistName() {
		return artistName;
	}

	/**
	 * Returns the number of tracks included on the album.
	 * 
	 * @return the number of tracks.
	 */
	public int getTrackCount() {
		return trackCount;
	}

	/**
	 * Returns the total size (in bytes) of the tracks included on the album.
	 * 
	 * @return the total size.
	 */
	public long getSize() {
		return size;
	}

	/**
	 * Returns the release date of the album, or
	 * <code>null</cod> if the release date
	 * is unknown.
	 * 
	 * @return the release date.
	 */
	public Date getReleaseDate() {
		return releaseDate;
	}

	/**
	 * Returns the purchase date of the album, or
	 * <code>null</cod> if the purchase date
	 * is unknown.
	 * 
	 * @return the purchase date.
	 */
	public Date getPurchaseDate() {
		return purchaseDate;
	}

	/**
	 * Returns the status code indicating if the album is associated with
	 * album's art.
	 * 
	 * @return the album's art status code.
	 */
	public int getHasArt() {
		return hasArt;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return getId().hashCode();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj) {
		if (!(obj instanceof Album)) {
			return false;
		}
		return getId().equals(((Album) obj).getId());
	}

}